#!/usr/bin/bash

PATH=/opt/local/gnu/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/usr/sbin

# Exit if any commands fail
set -o errexit

# Configuring image specific packages
echo "* Configuring image specific packages.";

# download server, original from: https://github.com/MerlinDMC/dsapid/releases/
DSPID_VERSION='0.7.1'
curl -L https://minio.itime.biz:4443/downloads/dsapid/dsapid-server-${DSPID_VERSION}.tar.bz2 | gtar xj -C /opt/dsapid/
mv /opt/dsapid/dsapid-server-${DSPID_VERSION}/server-sunos /opt/dsapid/server

chmod +x /opt/dsapid/server

/usr/sbin/groupadd -g 5092 dsapid
/usr/sbin/useradd -u 5092 -g dsapid -d /opt/dsapid -s /usr/bin/bash dsapid

if ! projects -l dsapid >/dev/null 2>&1; then
  LANG=C projadd -U dsapid -G dsapid \
    -K "process.max-file-descriptor=(basic,16384,deny)" \
    -c "Dataset Image Server" dsapid
fi

echo "* Create nginx ssl folder"
mkdir -p /opt/local/etc/nginx/ssl

# Clean up
echo "* Cleaning up."
pkgin clean
rm -rf /root/*
rm -rf /opt/dsapid/dsapid-server-${DSPID_VERSION}

cat > /opt/local/etc/rsyslog.conf << EOF
#
# Sample rsyslog configuration file
#

$ModLoad immark
$ModLoad imsolaris
# $ModLoad imtcp
# $ModLoad imudp

*.err;kern.notice;auth.notice                   /dev/sysmsg
*.err;kern.debug;daemon.notice;mail.crit        /var/adm/messages
*.alert;kern.err;daemon.err                     :omusrmsg:operator
*.alert                                         :omusrmsg:root
*.emerg                                         :omusrmsg:*

mail.debug                                      /var/log/syslog
mail.info                                       /var/log/maillog
auth.info                                       /var/log/authlog
EOF

# Prepare image for provisioning
sm-prepare-image -y
